export function isKorean(text) {
    const reg = /([(\uAC00-\uD7AF)|(\u3130-\u318F)])+/gi;
    return reg.test(text);
}

// scrollTop animation
export function scrollTop(el, from = 0, to, duration = 500, endCallback) {
    if (!window.requestAnimationFrame) {
        window.requestAnimationFrame =
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback) {
                return window.setTimeout(callback, 1000 / 60);
            };
    }
    const difference = Math.abs(from - to);
    const step = Math.ceil((difference / duration) * 50);

    function scroll(start, end, step) {
        if (start === end) {
            endCallback && endCallback();
            return;
        }

        let d = start + step > end ? end : start + step;
        if (start > end) {
            d = start - step < end ? end : start - step;
        }

        if (el === window) {
            window.scrollTo(d, d);
        } else {
            el.scrollTop = d;
        }
        window.requestAnimationFrame(() => scroll(d, end, step));
    }
    scroll(from, to, step);
}

// 判断参数是否是其中之一
export function oneOf(value, validList) {
    for (let i = 0; i < validList.length; i++) {
        if (value === validList[i]) {
            return true;
        }
    }
    return false;
}

//向上查找组件
export function findComponentUpward(context, componentName, componentNames) {
    if (typeof componentName === "string") {
        componentNames = [componentName];
    } else {
        componentNames = componentName;
    }

    let parent = context.$parent;
    let name = parent.$options.name;
    while (parent && (!name || componentNames.indexOf(name) < 0)) {
        parent = parent.$parent;
        if (parent) name = parent.$options.name;
    }
    return parent;
}

export function findComponentDownward(context, componentName) {
    const $children = context.$children;
    let children = null;

    if ($children.length) {
        for (const child of $children) {
            const name = child.$options.name;
            if (name === componentName) {
                children = child;
                break;
            } else {
                children = findComponentDownward(child, componentName);
                if (children) break;
            }
        }
    }
    return children;
}

export const generateId = function () {
    return Math.floor(Math.random() * 10000);
};

/**
 * addClass - 添加类
 * @param  dom el
 * @param  string cls 要添加的类名
 * */
export const addClass = (el, cls) => {
    if (!el) {
        return;
    }
    let curClass = el.className;
    let classes = (cls || "").split(" ");

    for (var i = 0, j = classes.length; i < j; i++) {
        let clsName = classes[i];
        if (!clsName) {
            continue;
        }

        if (el.classList) {
            el.classList.add(clsName);
        } else if (!hasClass(el, clsName)) {
            curClass += " " + clsName;
        }
    }
    if (!el.classList) {
        el.className = curClass;
    }
};

/**
 * removeClass - 删除类
 * @param  dom el
 * @param  string cls 要删除的类名
 * */
export const removeClass = (el, cls) => {
    if (!el || !cls) {
        return;
    }
    let classes = cls.split(" ");
    let curClass = " " + el.className + " ";

    for (let i = 0, j = classes.length; i < j; i++) {
        let clsName = classes[i];
        if (!clsName) {
            continue;
        }

        if (el.classList) {
            el.classList.remove(clsName);
        } else if (hasClass(el, clsName)) {
            curClass = curClass.replace(" " + clsName + " ", " ");
        }
    }
    if (!el.classList) {
        el.className = trim(curClass);
    }
};

/**
 * ifHex - 判断是否时hex格式
 * */
export const ifHex = (val) => {
    return typeof val === "string" &&
        /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(val)
        ? true
        : false;
};

/**
 * getScrollTop
 * */
export const getScrollTop = function () {
    var scroll_top = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scroll_top = document.documentElement.scrollTop;
    } else if (document.body) {
        scroll_top = document.body.scrollTop;
    }
    return scroll_top;
};

/**
 * getScrollLeft
 * */
export const getScrollLeft = function () {
    var scroll_left = 0;
    if (document.documentElement && document.documentElement.scrollLeft) {
        scroll_left = document.documentElement.scrollLeft;
    } else if (document.body) {
        scroll_left = document.body.scrollLeft;
    }
    return scroll_left;
};


export const isFunction = function(functionToCheck) {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}